﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;

namespace LotteCinema
{
    public partial class QuanLyMaLoaiGhe : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
                LaydsLG();
        }

        public void LaydsLG()
        {
            string ten="";
            DataTable ds_lg = Timkiem_Ten(ten);
        
            for (int i = 0; i < ds_lg.Rows.Count; i++)
            {
                string str = ds_lg.Rows[i]["Malg"].ToString();
                DDLMaLG.Items.Add(str);
            }
            grLoaiGhe.DataSource = ds_lg;
            grLoaiGhe.DataBind();
        }

        public DataTable Timkiem_Ten(string ten)
        {
            try
            {
                if (ten != null || ten != "")
                {
                    List<SqlParameter> sqlPara = new List<SqlParameter>();
                    sqlPara.Add(new SqlParameter("@tenlg", ten));
                    return CDatastore.DocStore("SP_TIMKIEM_TENLG", sqlPara);
                }
            }
            catch (Exception e)
            {
                lblThongBao.Text = e.Message;
            }
            return CDatastore.DocStore("SP_TIMKIEM_TENLG");
        }
        public DataTable Timkiem_MA(string ten)
        {
            try
            {
                if (ten != null || ten != "")
                {
                    List<SqlParameter> sqlPara = new List<SqlParameter>();
                    sqlPara.Add(new SqlParameter("@MAlg", ten));
                    return CDatastore.DocStore("SP_TIMKIEM_MALG", sqlPara);
                }
            }
            catch (Exception e)
            {
                lblThongBao.Text = e.Message;
            }
            return CDatastore.DocStore("SP_TIMKIEM_MALG");
        }
        protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                List<SqlParameter> sqlPara = new List<SqlParameter>();
                sqlPara.Add(new SqlParameter("@malg", DDLMaLG.Text.ToString()));
                CDatastore.ThucHienStore("SP_DELETE_LOAIGHE", sqlPara);
                lblThongBao.Text = "Bạn đã xóa Mã Loại Ghế " + DDLMaLG.Text + " thành công";
                DDLMaLG.Items.Clear();
                LaydsLG();
                txtTenLoaiGhe.Text = null;
            }
            catch (Exception ex)
            {
                lblThongBao.Text = ex.Message;
            }
        }

        protected void DDLMaLG_SelectedIndexChanged(object sender, EventArgs e)
        {
           // string s = "select * from loaighe where malg='" + DDLMaLG.SelectedItem.ToString() + "'";
            DataTable dt = Timkiem_MA(DDLMaLG.SelectedItem.ToString());
            if (dt.Rows.Count != 0)
            {
                txtTenLoaiGhe.Text = dt.Rows[0][1].ToString();
            }
        }

        protected void grLoaiGhe_SelectedIndexChanged(object sender, EventArgs e)
        {
            DDLMaLG.Text = grLoaiGhe.SelectedRow.Cells[1].Text;
            txtTenLoaiGhe.Text = grLoaiGhe.SelectedRow.Cells[2].Text;
        }

        protected void imThem_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                List<SqlParameter> sqlPara = new List<SqlParameter>();
                sqlPara.Add(new SqlParameter("@tenlg", txtTenLoaiGhe.Text));
                CDatastore.ThucHienStore("SP_INSERT_LOAIGHE", sqlPara);
                lblThongBao.Text = "Bạn đã thêm Ca Chiếu thành công";
                DDLMaLG.Items.Clear();
                txtTenLoaiGhe.Text = null;
                LaydsLG();
            }
            catch (Exception ex)
            {
                lblThongBao.Text = ex.Message;
            }
        }

        protected void imSua_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                List<SqlParameter> sqlPara = new List<SqlParameter>();
                sqlPara.Add(new SqlParameter("@malg", DDLMaLG.Text.ToString()));
                sqlPara.Add(new SqlParameter("@tenlg", txtTenLoaiGhe.Text));
                CDatastore.ThucHienStore("SP_UPDATE_LOAIGHE", sqlPara);
                lblThongBao.Text = "Bạn đã cập nhật Mã Loại Ghế " + DDLMaLG.Text + " thành công";
                DDLMaLG.Items.Clear();
                LaydsLG();
                txtTenLoaiGhe.Text = null;
            }
            catch (Exception ex)
            {
                lblThongBao.Text = ex.Message;
            }
        }

        protected void btTimKiem_Click(object sender, EventArgs e)
        {
            DataTable DT = Timkiem_Ten(txtTenLoaiGhe.Text);
            grLoaiGhe.DataSource = DT;
            grLoaiGhe.DataBind();
        }






    }
}